<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * Code to create orders by ketu.lai
 */





 $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        if ($customer->getCustomerId()) {
            $quote->assignCustomer($customer);
        } else {
            $quote->setCustomerEmail('customer@example.com');
        }

#$quote->setIsSuperMode(true);

        $productSku = array('sku1000', 'sku1001');
        foreach ($productSku as $sku) {
            $product = Mage::getModel('catalog/product');
            $product->load($product->getIdBySku($sku));

            $buyInfo = array(
                $product->getId() => array(
                    'qty' => 2),
            );
            $quote->addProduct($product, new Varien_Object($buyInfo));
        }




        $addressData = array(
            'firstname' => 'Test',
            'lastname' => 'Test',
            'street' => 'Sample Street 10',
            'city' => 'Somewhere',
            'postcode' => '123456',
            'telephone' => '123456',
            'country_id' => 'HK',
            //'region_id' => 12, // id from directory_country_region table
        );

        $billingAddress = $quote->getBillingAddress()->addData($addressData);
        $shippingAddress = $quote->getShippingAddress()->addData($addressData);
        $shippingAddress->setRegion('Guangdong');
        $billingAddress->setRegion('Guangdong');
        $shippingAddress->setCollectShippingRates(true);
        $quote->setCouponCode('44444');

        $shippingAddress->setShippingMethod('flatrate_flatrate');



        $quote->getPayment()->importData(array('method' => 'checkmo'));

        #echo $quote->getItemsCount();
        $quote->collectTotals()->save();


        /*
        echo $shippingAddress->getDiscountAmount();
        echo $quote->getGrandTotal();
        $_rates = $shippingAddress->getGroupedAllShippingRates();
        foreach ($_rates as $code => $rates) {
            foreach ($rates as $rate) {
                echo $rate->getMethodTitle(), $rate->getPrice(), '<br />';
            }
        }
         *
         */


        $service = Mage::getModel('sales/service_quote', $quote);
        $service->submitAll();
        $order = $service->getOrder();
        printf("Created order %s\n", $order->getIncrementId());

?>
